/**
 * 获取cookie
 */

export const getCookie = name => {
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //正则匹配
        if (arr = document.cookie.match(reg)) {
            return unescape(arr[2]);
        } else {
            return null;
        }
    }
    /**
     * 删除cookie
     */
export const delCookie = name => {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = getCookie(name);
        if (cval != null) {
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
        }
    }
    /**
     * 设置cookie
     */
export const setCookie = (name, value) => {
        var Days = 3;
        var exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
    }
    /**
     * 存储localStorage
     */
export const setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage Json
 */
export const getStoreJson = name => {
        if (!name) return;
        let content = window.localStorage.getItem(name);
        content = JSON.parse(content);
        return content;
    }
    /**
     * 获取localStorage
     */
export const getStore = name => {
        if (!name) return;
        return window.localStorage.getItem(name);
    }
    /**
     * 删除localStorage
     */
export const removeStore = name => {
        if (!name) return;
        window.localStorage.removeItem(name);
    }
    /**
     * 时间戳转YY-MM-DD HH:MM:SS
     */
export const formatDateTime = time => {
    if (!time) return;
    let date = new Date(time);
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let minute = date.getMinutes();
    let second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}
export const formatDateTimeSprit = time => {
        if (!time) return;
        let date = new Date(time);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        let minute = date.getMinutes();
        let second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '/' + m + '/' + d + ' ' + h + ':' + minute + ':' + second;
    }
    /**
     * 时间戳转YY-MM-DD HH:MM:SS
     */
export const formatDateTimeShort = time => {
    if (!time) return;
    let date = new Date(time);
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + m + '-' + d;
}
export const formatDateTimeShortSprit = time => {
        if (!time) return;
        let date = new Date(time);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        return y + '/' + m + '/' + d;
    }
    /**
     * 电话号码验证
     */
export const checkTel = (rule, value, callback) => {
    const mobile = /^1[3|5|7|8]\d{9}$/,
        phone = /^0\d{2,3}-?\d{7,8}$/;
    const status = mobile.test(value) || phone.test(value);
    if (!value) {
        return callback(new Error('电话不能为空'));
    } else if (!status) {
        return callback(new Error('电话格式不对啊'));
    } else {
        callback();
    }
}